﻿using AutoMapper;
using AutoMapper.QueryableExtensions;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_LineBusiness : BaseBusiness<PB_Line>, IPB_LineBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        readonly IMapper _mapper;
        public PB_LineBusiness(IDbAccessor db, IMapper mapper, IOperator @operator)
            : base(db)
        {
            _mapper = mapper;
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_LineDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var search = input.Search;
            Expression<Func<PB_Line, Base_Department, PB_LineDTO>> select = (a, b) => new PB_LineDTO
            {
                DepartmentName = b.Name
            };
          
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DeptId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);

            if (!search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x =>
                      EF.Functions.Like(x.Name, keyword)
                      || EF.Functions.Like(x.Code, keyword));
            }
            q=q.Where(x=>_operator.Property.DeptIds.Contains(x.DeptId));
            return await q.GetPageResultAsync(input);
        }

        public async Task<PB_Line> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }
        [DataRepeatValidate(new string[] { "Name" }, new string[] { "名字 "})]
        public async Task AddDataAsync(PB_Line data)
        {
            await InsertAsync(data);
        }
        [DataRepeatValidate(new string[] { "Name" }, new string[] { "名字 " })]
        public async Task UpdateDataAsync(PB_Line data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<PageResult<PB_LineInfoDTO>> GetDataListAsyncs(PageInput<PB_LineInputDTO> input)
        {
            var where = LinqHelper.True<PB_Line>();
            var search = input.Search;
            if (!search.LineId.IsNullOrEmpty())
                where = where.And(x => x.Id == search.LineId);
            if (!search.LineId.IsNullOrEmpty())
                where = where.And(x => x.Id.Contains(search.LineId));

            var page = await GetIQueryable()
                .Where(where)
                .ProjectTo<PB_LineInfoDTO>(_mapper.ConfigurationProvider)
                .GetPageResultAsync(input);

            await SetProperty(page.Data);

            return page;

            async Task SetProperty(List<PB_LineInfoDTO> _list)
            {
                var allActionIds = await Db.GetIQueryable<PB_Procecure>().Select(x => x.Id).ToListAsync();

                var ids = _list.Select(x => x.Id).ToList();
                var roleActions = await Db.GetIQueryable<PB_LineProcecure>()
                    .Where(x => ids.Contains(x.LineId))
                    .ToListAsync();
                _list.ForEach(aData =>
                {
                   
                        aData.Actions = roleActions.Where(x => x.LineId == aData.Id).Select(x => x.ProcecureId).ToList();
                });
            }
        }

        public  async Task<PB_LineInfoDTO> GetTheDataAsyncS(string id)
        {
            return (await GetDataListAsyncs(new PageInput<PB_LineInputDTO> { Search = new PB_LineInputDTO { LineId = id } })).Data.FirstOrDefault();
        }


        #endregion

        #region 私有成员

        #endregion
    }
}